package com.doraz.orb.ui;

import java.util.List;

import com.doraz.orb.structs.*;

import com.doraz.orb.R;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.text.Layout;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class OrbObjectAdapter extends ArrayAdapter<OrbObject> {
	private static String TAG = "OBJECT ARRAY ADAPTER";
	
	public OrbObjectAdapter(Context context, int textViewResourceId) {
		super(context, textViewResourceId);
	}

	public OrbObjectAdapter(Context context, int resource,
			int textViewResourceId, List<OrbObject> objects) {
		super(context, resource, textViewResourceId, objects);
	}

	public OrbObjectAdapter(Context context, int resource,
			int textViewResourceId, OrbObject[] objects) {
		super(context, resource, textViewResourceId, objects);
	}

	public OrbObjectAdapter(Context context, int resource,
			int textViewResourceId) {
		super(context, resource, textViewResourceId);
	}

	public OrbObjectAdapter(Context context, int textViewResourceId,
			List<OrbObject> objects) {
		super(context, textViewResourceId, objects);
	}

	public OrbObjectAdapter(Context context, int textViewResourceId,
			OrbObject[] objects) {
		super(context, textViewResourceId, objects);
	}
	
	
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		OrbObject obj = getItem(position);
		//Get the view if not supplied
		View v = convertView;
        if (v == null) {
                LayoutInflater vi = (LayoutInflater)new Activity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                v = vi.inflate(R.layout.orbobject_layout, null);
        }
		
		TextView objTextView = (TextView) v.findViewWithTag("ObjectText");
		ImageView objImageView = (ImageView) v.findViewWithTag("ObjectImage");
		//Set the view based on the object type
		switch(obj.getObjectType()) {
			case OrbObject.ORB_GROUP: {
				Group group = (Group) obj;
				//Set the text
				if(group.getTitle() != null){
					objTextView.setText(group.getTitle());
				}
				else if(group.getPath() != null) {
					objTextView.setText(group.getPath());
				}
				
				//Set the icon
				objImageView.setImageResource(R.drawable.folder_ico);
				
				break;
			}
			
			case OrbObject.ORB_ITEM: {
				OrbItem item = (OrbItem) obj;
				//Set the text
				if(item.getTitle() != null) {
					objTextView.setText(item.getTitle());
				}
				else if(item.getPath() != null) {
					objTextView.setText(item.getPath());
				}
				
				//Set the icon
				if(item.isAudio()) {
					objImageView.setImageResource(R.drawable.audio_ico);
				}
				else if(item.isVideo()) {
					objImageView.setImageResource(R.drawable.video_ico);
				}
				else {
					objImageView.setImageResource(R.drawable.file_ico);
				}
				
				break;
			}
			
			case OrbObject.ORB_COMMONMETHOD: {
				CommonMethod method = (CommonMethod) obj;
				objTextView.setText(method.getTitle());
				objImageView.setImageResource(method.getDrawableId());
				break;
			}
		}
		
		return v;
	}
}
